﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RbmlVisualizer.Data;

namespace RbmlVisualizer.Utilities
{
    public static class MultiplicityConverter
    {
        public static string ConvertToString(Multiplicity multiplicity)
        {
            switch (multiplicity)
            {
                case Multiplicity.None:
                    return "";
                case Multiplicity.Zero:
                    return "0";
                case Multiplicity.One:
                    return "1";
                case Multiplicity.Many:
                    return "*";
                case Multiplicity.OneToMany:
                    return "1..*";
                case Multiplicity.ZeroToMany:
                    return "0..*";
                case Multiplicity.ZeroToOne:
                    return "0..1";
                default:
                    throw new InvalidOperationException("You can't convert the multiplicity you passed in." + multiplicity);
            }
        }

        public static Multiplicity ConvertFromString(string multiplicityString)
        {
            if (string.IsNullOrEmpty(multiplicityString))
                return Multiplicity.None;

            var multiplicity = Multiplicity.None;

            var characters = multiplicityString.ToCharArray();
            foreach (var c in characters)
            {
                switch (c)
                {
                    case '1':
                        multiplicity |= Multiplicity.One;
                        break;
                    case '0':
                        multiplicity |= Multiplicity.Zero;
                        break;
                    case '*':
                        multiplicity |= Multiplicity.Many;
                        break;
                }
            }
            return multiplicity;
        }
    }
}
